//
//  ContactViewController.m
//  Showing
//
//  Created by Ralf on 4/9/15.
//  Copyright (c) 2015 next-software. All rights reserved.
//

@import CoreData;

#import "ContactViewController.h"
#import "ContactViewCell.h"
#import "AppDelegate.h"
#import "ChatViewController.h"
#import "MessageManager.h"

@interface ContactViewController ()
{
    AppDelegate    *appDelegate;
    MessageManager *messageManager;
}
@end

@implementation ContactViewController

@synthesize contacts;

- (void)viewDidLoad
{
    [super viewDidLoad];

    appDelegate    = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    messageManager = [MessageManager new];

    [self loadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    appDelegate.rootController.navigationItem.rightBarButtonItem = nil;
    appDelegate.rootController.navigationItem.leftBarButtonItem = nil;
    appDelegate.rootController.navigationItem.titleView          = nil;
    appDelegate.rootController.navigationItem.title              = @"消息";
    [super viewWillAppear:animated];
    [self loadData];
    [self.tableView reloadData];

}

- (void)loadData
{
    self.contacts = nil;

    if (![appDelegate isLogged])
    {
        return;
    }

    ResultSet *rs = [messageManager queryContacts];

    if ([self verifyResutSet:rs])
    {
        self.contacts = [rs toArray];
        [self.tableView reloadData];
    }
}

- (IBAction)reloadData:(id)sender
{
    [self loadData];
    [super reloadData:sender];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;

    if (self.contacts && self.contacts.count > 0)
    {
        count = self.contacts.count;
    }

    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContactViewCell *cell = [ContactViewCell cellWithTableView:tableView];
    [cell setData:self.contacts[indexPath.row]];

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.navigationController.interactivePopGestureRecognizer.enabled  = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    //    MessageViewController* vcMessage = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
    //    vcMessage.contact = self.contacts[indexPath.row][@"contact"];
    //    [self.navigationController pushViewController:vcMessage animated:YES];
    ChatViewController *controller = [[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
    controller.contact = self.contacts[indexPath.row][@"contact"];
    [self.navigationController pushViewController:controller animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

@end
